<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <%-- 页头 --%>
    <title>更新视频分类 - ReadJava教程</title>
    <%@include file="../common/head.jsp" %>
    <link rel="stylesheet" href="/resources/editor/css/editormd.min.css"/>
</head>
<body>
<div class="wrapper">

    <%-- 导航 --%>
    <%@include file="manage-header.jsp" %>

    <%-- 侧边栏 --%>
    <%@include file="manage-sidebar.jsp" %>

    <div class="main-panel">
        <div class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-header">
                                <div class="card-title">更新视频分类</div>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>视频编号</label>
                                            <input type="text" name="acId" value="${videoCategory.vcId}"
                                                   disabled class="form-control input-square vcId">
                                        </div>
                                        <div class="form-group">
                                            <label>视频标题</label>
                                            <input type="text" name="acTitle"
                                                   value="${videoCategory.vcTitle}"
                                                   class="form-control input-square vcTitle">
                                        </div>
                                        <div class="form-group text-center">
                                            <label class="form-radio-label">
                                                <input class="form-radio-input vcState" type="radio"
                                                       name="acState" value="0">
                                                <span class="form-radio-sign">启用分类</span>
                                            </label>
                                            <label class="form-radio-label">
                                                <input class="form-radio-input vcState" type="radio"
                                                       name="acState" value="1">
                                                <span class="form-radio-sign">禁用分类</span>
                                            </label>
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>视频价格</label>
                                            <input type="text" name="acIcon"
                                                   value="${videoCategory.vcPrice}"
                                                   class="form-control input-square vcPrice">
                                        </div>
                                        <div class="form-group">
                                            <label>视频图标</label>
                                            <input type="text" name="acIcon"
                                                   value="${videoCategory.vcIcon}"
                                                   class="form-control input-square vcIcon">
                                        </div>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="form-group">
                                            <label>视频目录</label>
                                            <div id="test-editormd">
                                                <textarea style="display:none;" id="textContent"
                                                          name="vcContent">${videoCategory.vcContent}</textarea>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="card-action text-center">
                                <button type="button" class="btn btn-info btn-success" id="updateVcBtn">
                                    更新分类
                                </button>
                                <button type="button" class="btn btn-info btn-danger"
                                        onclick="window.location.href='/manage/video/category/list'">
                                    返回管理
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<%-- 页脚 --%>
<%@include file="../common/footer.jsp" %>

</div>
</body>

<script src="/resources/assets/js/core/jquery.3.2.1.min.js"></script>
<script src="/resources/editor/lib/marked.min.js"></script>
<script src="/resources/editor/lib/prettify.min.js"></script>
<script src="/resources/editor/lib/raphael.min.js"></script>
<script src="/resources/editor/lib/underscore.min.js"></script>
<script src="/resources/editor/lib/sequence-diagram.min.js"></script>
<script src="/resources/editor/lib/flowchart.min.js"></script>
<script src="/resources/editor/lib/jquery.flowchart.min.js"></script>
<script src="/resources/editor/editormd.min.js"></script>
<script>
    var testEditor;
    $(function () {
        testEditor = editormd("test-editormd", {
            height: 600,
            htmlDecode: "video",  // 你可以过滤标签解码
            syncScrolling: "single",
            path: "/resources/editor/lib/",
            saveHTMLToTextarea: true,
            imageUpload: true,
            imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
            imageUploadURL: "/ftp/upload",
        });

        // 选中分类状态
        var vcState = parseInt('${videoCategory.vcState}');
        switch (vcState) {
            case 0:
                $(".vcState").eq(0).attr('checked', true);
                break;
            case 1:
                $(".vcState").eq(1).attr('checked', true);
                break;
            default:
                alert("设置选中状态bug");
        }

        // 更新视频分类
        $("#updateVcBtn").on("click", function () {
            var vcId = $(".vcId").val();
            var vcTitle = $(".vcTitle").val();
            var vcPrice = $(".vcPrice").val();
            var vcIcon = $(".vcIcon").val();
            var vcState = $('.vcState:checked').val();
            var vcContent = testEditor.getMarkdown();
            if (vcId != null && vcId.length != 0) {
                if (vcTitle != null && vcTitle.length != 0) {
                    if (vcPrice != null && vcPrice.length != 0) {
                        if (vcIcon != null && vcIcon.length != 0) {
                            if (vcState != null && vcState.length != 0) {
                                if (vcContent != null && vcContent.length != 0) {
                                    $.ajax({
                                        type: "POST",
                                        url: "/manage/video/category/update",
                                        data: {
                                            vcId: vcId,
                                            vcTitle: vcTitle,
                                            vcPrice: vcPrice,
                                            vcIcon: vcIcon,
                                            vcState: vcState,
                                            vcContent: vcContent
                                        },
                                        dataType: "json",
                                        success: function (data) {
                                            if (data.status == 200) {
                                                alert(data.message);
                                                var vc = data.videoCategory;
                                                $(".vcId").val(vc.vcId);
                                                $(".vcTitle").val(vc.vcTitle);
                                                $(".vcPrice").val(vc.vcPrice);
                                                $(".vcIcon").val(vc.vcIcon);
                                                $("#textContent").val(vc.vcContent);
                                                switch (vc.vcState) {
                                                    case 0:
                                                        $(".vcState").eq(0).attr('checked', true);
                                                        break;
                                                    case 1:
                                                        $(".vcState").eq(1).attr('checked', true);
                                                        break;
                                                    default:
                                                        alert("设置选中状态bug");
                                                }
                                            }
                                        },
                                        error: function () {
                                            alert("操作失败：Ajax更新错误");
                                        }
                                    });
                                } else {
                                    alert("操作失败：视频目录不能为空");
                                }
                            } else {
                                alert("操作失败：未设置视频状态");
                            }
                        } else {
                            alert("操作失败：视频图标不能为空");
                        }
                    } else {
                        alert("操作失败：视频价格不能为空");
                    }
                } else {
                    alert("操作失败：视频标题不能为空");
                }
            } else {
                alert("操作失败：未设置视频状态");
            }
        });
    });
</script>
</html>